package com.hjf.ProjectATM;

import com.hjf.javabeandemo.Movie;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    private Account loginAcc; //用来记住登录的账户

    public void start(){
        Account a = new Account("1","hjf",'男',"123123",2000,1000);
        Account b = new Account("2","sys",'女',"123123",2000,1000);
        accounts.add(a);
        accounts.add(b);
        while (true) {
            System.out.println("===Welcome to ATM System===");
            System.out.println("1.Login in");
            System.out.println("2.Sign up");
            System.out.println("Please choose:");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    // Login in
                    login();
                    break;
                case 2:
                    createAccount();
                    break;
                default:
                    System.out.println("Error Operation!");
            }
        }
    }

    private void login(){
        System.out.println("=====Login in=====");
        if(accounts.size() == 0){
            System.out.println("no account!");
            return;
        }

        while (true) {
            System.out.println("please input your cardId:");
            String cardId = sc.next();

            Account acc = getAccountByCardId(cardId);

            if(acc == null){
                System.out.println("CardId " + cardId + "does not exist!");
            }
            else{
                while (true) {
                    System.out.println("please input your password!");
                    String password = sc.next();
                    if (acc.getPassWord().equals(password)){
                        System.out.println(acc.getUserName() + " login in successfully! Your cardId is: " + acc.getCardId());
                        loginAcc = acc;
                        showUserCommand();
                        return;
                    }
                    else{
                        System.out.println("the password your input is wrong, please input again!");
                    }
                }
            }
        }

    }

    private void showUserCommand(){
        System.out.println(loginAcc.getUserName() + "Please choose your operation:");
        while (true) {
            System.out.println("1.查询信息.");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.密码修改");
            System.out.println("6.退出");
            System.out.println("7.注销当前账户");

            int command = sc.nextInt();
            switch (command){
                case 1:
                    showLoginAccount();
                    break;
                case 2:
                    depositMoney();
                    break;
                case 3:
                    drawMoney();
                    break;
                case 4:
                    transformMoney();
                    break;
                case 5:
                    updatePassword();
                    return;
                case 6:
                    System.out.println(loginAcc.getUserName() + "退出系统成功！");
                    return;
                case 7:
                    if(deleteAccount()){
                        return;
                    }
                    break;
                default:
                    System.out.println("输入不合法，请重新输入！");
            }
        }
    }

    private void updatePassword() {
        System.out.println("====修改密码====");
        while (true) {
            System.out.println("请输入您的原始密码！");
            String password = sc.next();
            if(loginAcc.getPassWord().equals(password)){
                while (true) {
                    System.out.println("请输入新密码！");
                    String newPassWord = sc.next();
                    System.out.println("请再次输入密码！");
                    String newPassword2 = sc.next();
                    if(newPassWord.equals(newPassword2)){
                        loginAcc.setPassWord(newPassWord);
                        System.out.println("密码修改成功！");
                        return;
                    }else{
                        System.out.println("两次输入密码不一致！");
                    }
                }
            }
            else {
                System.out.println("您输入的密码有误！");
            }
        }
    }

    private boolean deleteAccount() {
        System.out.println("====销户操作====");
        System.out.println("请问你确认要销户吗？y/n");
        String command = sc.next();
        switch (command){
            case "y":
                if(loginAcc.getMoney()==0){
                    accounts.remove(loginAcc);
                    System.out.println("销户成功！");
                    return true;
                }
                else{
                    System.out.println("不好意思，您的账户中还有钱，不能销户！");
                    return false;
                }
            default:
                System.out.println("好的，保留您的账户!");
                return false;
        }
    }

    private void transformMoney() {
        System.out.println("====转账====");

        if (accounts.size() < 2){
            System.out.println("当前系统中无可转账用户！");
            return;
        }

        while (true) {
            System.out.println("请输入要转账的账户：");
            String accId = sc.next();
            Account acc = getAccountByCardId(accId);
            if(acc==null){
                System.out.println("要转账的账户不存在！");
            }
            else if(acc.getCardId().equals(loginAcc.getCardId())){
                System.out.println("无法给自己转账!");
            }
            else{
                String name = "*" + acc.getUserName().substring(1);
                System.out.println("请输入要转账的户主姓氏：" + name);
                String preName = sc.next();
                if (acc.getUserName().startsWith(preName)){
                    while (true) {
                        System.out.println("请输入转账金额！");
                        Double money = sc.nextDouble();
                        if (loginAcc.getMoney() < money){
                            System.out.println("余额不足！剩余：" + loginAcc.getMoney() + "元");
                        }
                        else{
                            if(loginAcc.getLimit() < money){
                                System.out.println("当前取款金额超过限额，限额为：" + loginAcc.getLimit());
                            }else{
                                loginAcc.setMoney(loginAcc.getMoney()-money);
                                System.out.println("转账成功！");
                                System.out.println("余额为："+loginAcc.getMoney());
                                acc.setMoney(acc.getMoney()+ money);
                                return;
                            }
                        }
                    }
                }
                else{
                    System.out.println("您认证的姓氏有问题！");
                }
            }
        }
    }

    private void showLoginAccount(){
        System.out.println("=====当前您的账户信息为=====");
        System.out.println("卡号："+ loginAcc.getCardId());
        System.out.println("姓名："+ loginAcc.getUserName());
        System.out.println("性别："+loginAcc.getSex());
        System.out.println("余额："+ loginAcc.getMoney());
        System.out.println("取款限额："+ loginAcc.getLimit());
    }

    private void depositMoney(){
        System.out.println("====存钱====");
        System.out.println("请输入存款金额");
        double money = sc.nextDouble();

        loginAcc.setMoney(loginAcc.getMoney() + money);
        System.out.println("存钱成功！余额为："+loginAcc.getMoney());

    }

    private void drawMoney(){
        System.out.println("====取钱====");
        if(loginAcc.getMoney() == 0){
            System.out.println("账户余额不足！");
            return;
        }

        while (true) {
            double money = sc.nextDouble();
            if (loginAcc.getMoney() < money){
                System.out.println("余额不足！剩余：" + loginAcc.getMoney() + "元");
            }
            else{
                if(loginAcc.getLimit() < money){
                    System.out.println("当前取款金额超过限额，限额为：" + loginAcc.getLimit());
                }else{
                    loginAcc.setMoney(loginAcc.getMoney()-money);
                    System.out.println("取款成功");
                    System.out.println("余额为："+loginAcc.getMoney());
                    break;
                }
            }
        }
    }

    private void createAccount(){
        // create account
        System.out.println("=====create account=====");
        Account acc = new Account();

        // input user message
        System.out.println("please input your account name:");
        String name = sc.next();
        acc.setUserName(name);

        while (true) {
            System.out.println("please input your sex:");
            char sex = sc.next().charAt(0); // 取出第一个字符作为性别
            if(sex == '男' || sex == '女'){
                acc.setSex(sex);
                break;
            }
            else {
                System.out.println("ERROR SEX!");
            }
        }

        while (true) {
            System.out.println("please input your password.");
            String passWord = sc.next();
            System.out.println("please ensure your password.");
            String okWord = sc.next();

            if(okWord.equals(passWord)){
                acc.setPassWord(passWord);
                break;
            }
            else{
                System.out.println("The second password is not same to the first one.");
            }
        }

        System.out.println("please input your limit.");
        double limit = sc.nextDouble();
        acc.setLimit(limit);

        // 自动生成卡号，并且不能与里面的信息重复
        String newCardId = createCardId();
        acc.setCardId(newCardId);

        // save account
        accounts.add(acc);
        System.out.println("congratulate "+acc.getUserName()+"开户成功！您的卡号是"+acc.getCardId());
    }

    private String createCardId(){
        while (true) {
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++) {
                int num = r.nextInt(10);
                cardId += num;
            }

            Account acc = getAccountByCardId(cardId);
            if(acc == null){
                return cardId;
            }
        }
    }

    private Account getAccountByCardId(String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if(acc.getCardId().equals(cardId)){
                return acc;
            }
        }

        return null;
    }
}
